package com.lc.proxy;

import com.lc.executor.Executor;
import com.lc.executor.impl.DefaultExecutor;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;


public class DaoProxy implements InvocationHandler {

    private Executor executor = new DefaultExecutor();

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

        if (Object.class.equals(method.getDeclaringClass())) {
            return method.invoke(this, args);
        }

        Class<?> returnType = method.getReturnType();
        String methodName = method.getName();
        if (methodName.startsWith("select")) {
            return executor.executeQuery(returnType, method, args);
        } else if (methodName.startsWith("insert")) {
            return executor.executeUpdate(method,args);
        } else if (methodName.startsWith("update")) {
            return executor.executeUpdate(method,args);
        } else if (methodName.startsWith("delete")) {
            return executor.executeUpdate(method,args);
        }
        return null;
    }
}
